import sys
import datetime

sys.path.append("..")
import app
from common.request_tool import request, run
from model.dao_utils.original_covid19_map_wangyi_log_dao import *
from model.entity.original_covid19_map_wangyi_log import OriginalCovid19MapWangyiLog


header = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/101.0.4951.64 Safari/537.36 Edg/101.0.1210.47 "
}


@run
@request(url="https://c.m.163.com/ug/api/wuhan/app/data/list-total", header=header)
def start(resp):
    # 定义抓取时间
    start_date = datetime.datetime.now().strftime('%Y-%m-%d')
    rep_json = resp.json()
    if rep_json["code"] != 10000:
        app.App().log.error(
            "网易疫情数据接口请求错误 url:[https://c.m.163.com/ug/api/wuhan/app/data/list-total] 内容:[" + resp.text + "]")
        return
    area_tree = rep_json["data"]["areaTree"]
    # 增量更新 存储数据集记录
    res_list = []
    word_num = [0, 0, 0, 0, 0, 0]
    for area in area_tree:
        sub_area_tree = area["children"]
        # 添加父区域数据
        en = OriginalCovid19MapWangyiLog()
        en.address_name = area["name"]
        en.address_name_en = ""
        en.total_confirm = format_column_num(area["total"]["confirm"])
        en.total_dead = format_column_num(area["total"]["dead"])
        en.total_heal = format_column_num(area["total"]["heal"])
        en.today_confirm = format_column_num(area["today"]["confirm"])
        en.today_dead = format_column_num(area["today"]["dead"])
        en.today_heal = format_column_num(area["today"]["heal"])
        en.create_date = start_date
        res_list.append(en)
        # 累加构造世界数据
        word_num[0] += en.total_confirm
        word_num[1] += en.total_dead
        word_num[2] += en.total_heal
        word_num[3] += en.today_confirm
        word_num[4] += en.today_dead
        word_num[5] += en.today_heal
        # 判断是否有子区域数据
        if len(sub_area_tree) < 2:
            continue
        # 处理子区域数据
        for sub_area in sub_area_tree:
            sub_sub_area_tree = sub_area["children"]
            # 添加子区域数据
            en = OriginalCovid19MapWangyiLog()
            en.address_name = sub_area["name"]
            en.address_name_en = ""
            en.total_confirm = sub_area["total"]["confirm"]
            en.total_dead = sub_area["total"]["dead"]
            en.total_heal = sub_area["total"]["heal"]
            en.today_confirm = format_column_num(sub_area["today"]["confirm"])
            en.today_dead = format_column_num(sub_area["today"]["dead"])
            en.today_heal = format_column_num(sub_area["today"]["heal"])
            en.create_date = start_date
            res_list.append(en)
            # 判断是否有下级区域数据
            if len(sub_sub_area_tree) < 2:
                continue
            # 处理下级区域数据
            for sub_sub_area in sub_sub_area_tree:
                # 存储区域数据
                en = OriginalCovid19MapWangyiLog()
                en.address_name = sub_sub_area["name"]
                en.address_name_en = ""
                en.total_confirm = sub_sub_area["total"]["confirm"]
                en.total_dead = sub_sub_area["total"]["dead"]
                en.total_heal = sub_sub_area["total"]["heal"]
                en.today_confirm = format_column_num(sub_sub_area["today"]["confirm"])
                en.today_dead = format_column_num(sub_sub_area["today"]["dead"])
                en.today_heal = format_column_num(sub_sub_area["today"]["heal"])
                en.create_date = start_date
                res_list.append(en)
    insert_all(res_list)
    # 存储世界数据
    word_en = OriginalCovid19MapWangyiLog()
    word_en.address_name = "世界"
    word_en.total_confirm = word_num[0]
    word_en.total_dead = word_num[1]
    word_en.total_heal = word_num[2]
    word_en.today_confirm = word_num[3]
    word_en.today_dead = word_num[4]
    word_en.today_heal = word_num[5]
    word_en.create_date = start_date
    insert(word_en)


def format_column_num(column):
    if column is None:
        return 0
    if type(column).__name__ == 'int' and column < 0:
        return 0
    return column
